
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Ana
 */
public class v_principal extends javax.swing.JFrame {
conexion conecta;
    private JLabel jLabel1;
    private JTextField jTextField1;
    private JPanel panel_ingresar;
    private JLabel label_producto;
    private JTextField tf_codigo;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JTextField tf_nombre;
    private JComboBox cb_precio;
    private JLabel jLabel4;
    private JLabel title_formulario;
    private JLabel imagen;
    private JPanel panel_opciones;
    private JButton btn_grabar;
    private JButton btn_nuevo;
    private JButton btn_modificar;
    private JButton btn_borrar;
    private JButton btn_salir;
    public v_principal() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        panel_ingresar = new javax.swing.JPanel();
        label_producto = new javax.swing.JLabel();
        tf_codigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tf_nombre = new javax.swing.JTextField();
        cb_precio = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        title_formulario = new javax.swing.JLabel();
        imagen = new javax.swing.JLabel();
        panel_opciones = new javax.swing.JPanel();
        btn_grabar = new javax.swing.JButton();
        btn_nuevo = new javax.swing.JButton();
        btn_modificar = new javax.swing.JButton();
        btn_borrar = new javax.swing.JButton();
        btn_salir = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registrar Producto");
        setBackground(new java.awt.Color(255, 255, 0));
        setForeground(new java.awt.Color(255, 255, 0));

        panel_ingresar.setBackground(new java.awt.Color(51, 102, 0));
        panel_ingresar.setForeground(new java.awt.Color(0, 102, 0));

        label_producto.setText("Código_Producto:");

        jLabel2.setText("Nombre_Producto:");

        jLabel3.setText("Precio_Producto:");

        cb_precio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5.000", "10.000", "15.000", "20.000", "25.000", "30.000", "35.000", "40.000", "45.000", "50.000" }));

        jLabel4.setIcon(new javax.swing.ImageIcon("C:\\SOFTMAD\\imagen\\mad.jpg")); // NOI18N

        javax.swing.GroupLayout panel_ingresarLayout = new javax.swing.GroupLayout(panel_ingresar);
        panel_ingresar.setLayout(panel_ingresarLayout);
        panel_ingresarLayout.setHorizontalGroup(
            panel_ingresarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_ingresarLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(panel_ingresarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel_ingresarLayout.createSequentialGroup()
                        .addComponent(label_producto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panel_ingresarLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panel_ingresarLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cb_precio, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );
        panel_ingresarLayout.setVerticalGroup(
            panel_ingresarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_ingresarLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel_ingresarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(panel_ingresarLayout.createSequentialGroup()
                        .addGroup(panel_ingresarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_producto)
                            .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel_ingresarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tf_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(panel_ingresarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(cb_precio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        title_formulario.setFont(new java.awt.Font("Andalus", 3, 24)); // NOI18N
        title_formulario.setForeground(new java.awt.Color(0, 102, 0));
        title_formulario.setText("Maderera \"Los Verdes Group\"");

        imagen.setBackground(new java.awt.Color(255, 255, 255));
        imagen.setForeground(new java.awt.Color(255, 255, 0));
        imagen.setIcon(new javax.swing.ImageIcon("C:\\SOFTMAD\\imagen\\pino.jpg.png")); // NOI18N
        imagen.setToolTipText("");

        panel_opciones.setBackground(new java.awt.Color(51, 102, 0));

        btn_grabar.setIcon(new javax.swing.ImageIcon("C:\\SOFTMAD\\imagen\\accept.png")); // NOI18N
        btn_grabar.setText("Grabar");
        btn_grabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_grabarActionPerformed(evt);
            }
        });

        btn_nuevo.setIcon(new javax.swing.ImageIcon("C:\\SOFTMAD\\imagen\\icon-student.png")); // NOI18N
        btn_nuevo.setText("Nuevo");
        btn_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_nuevoActionPerformed(evt);
            }
        });

        btn_modificar.setIcon(new javax.swing.ImageIcon("C:\\SOFTMAD\\imagen\\update-20.png")); // NOI18N
        btn_modificar.setText("Modificar");

        btn_borrar.setIcon(new javax.swing.ImageIcon("C:\\SOFTMAD\\imagen\\excluded.png")); // NOI18N
        btn_borrar.setText("Borrar");
        btn_borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_borrarActionPerformed(evt);
            }
        });

        btn_salir.setIcon(new javax.swing.ImageIcon("C:\\SOFTMAD\\imagen\\salir.png")); // NOI18N
        btn_salir.setText("Salir");
        btn_salir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_salirMouseClicked(evt);
            }
        });
        btn_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_salirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel_opcionesLayout = new javax.swing.GroupLayout(panel_opciones);
        panel_opciones.setLayout(panel_opcionesLayout);
        panel_opcionesLayout.setHorizontalGroup(
            panel_opcionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_opcionesLayout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(panel_opcionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btn_modificar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_grabar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(panel_opcionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btn_nuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_borrar, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_opcionesLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btn_salir, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(72, 72, 72))
        );
        panel_opcionesLayout.setVerticalGroup(
            panel_opcionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_opcionesLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(panel_opcionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_nuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_grabar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panel_opcionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_modificar)
                    .addComponent(btn_borrar))
                .addGap(18, 18, 18)
                .addComponent(btn_salir)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(44, Short.MAX_VALUE)
                .addComponent(title_formulario, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel_ingresar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(imagen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(panel_opciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(8, 8, 8)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(title_formulario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel_ingresar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panel_opciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(imagen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btn_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_nuevoActionPerformed
// codigo para crear un nuevo registro
try
{
conecta.resulset.last();//primero hacemos la conexion y nos posicionamos en el último registro
int cod = conecta.resulset.getInt("codigo")+1;//Le sumo un 1 para
tf_codigo.setText("");//vacio el campo codigo
tf_nombre.setText("");//vacio el campo nombre
cb_precio.setSelectedItem("5000");//asigno por defecto el primer Departamento
tf_nombre.requestFocus();//Asigno el foco en Nombre
tf_codigo.setEditable(false);//Evito que el codigo sea editable
tf_codigo.setText(cod+"");//Agrego el valor del codigo
}
catch (SQLException e)
{
JOptionPane.showMessageDialog(null,"No es posible crear un nuevo registro "+e);
}
}    // codigo para crear un nuevo registro
    try {
        conecta.resulset.last();//primero hacemos la conexion y nos posicionamos en el último registro
    } catch (SQLException ex) {
        Logger.getLogger(v_principal.class.getName()).log(Level.SEVERE, null, ex);
    }
    int cod = conecta.resulset.getInt("codigo_producto")+1;//Le sumo un 1 para
    tf_codigo.setText("");//vacio el campo codigo
    tf_nombre.setText("");//vacio el campo nombre
    cb_precio.setSelectedItem("5000");//asigno por defecto el primer precio
    tf_nombre.requestFocus();//Asigno el foco en Nombre
    tf_codigo.setEditable(false);//Evito que el codigo sea editable
    tf_codigo.setText(cod+"");//Agrego el valor del codigo
    }
    catch (SQLException e)
    {
    JOptionPane.showMessageDialog(null,"No es posible crear un nuevo registro "+e);
 // TOdO add your handling code here:
    }//GEN-LAST:event_btn_nuevoActionPerformed

    private void btn_grabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_grabarActionPerformed
// Codigo para grabar datos dentro de la BD
try
{
//variable que guarda el String del codigo sql para realizar el insert
String sqlInsert = "insert into producto (codigo_producto,nombre_producto,precio_producto) values ("+
tf_codigo.getText()+",'"+
tf_nombre.getText()+"','"+
cb_precio.getSelectedItem()+"')";
//Primero conecto con la BD y luego ejecuto el codigo de actualizacion del lado de la BD
conecta.statement.executeUpdate(sqlInsert);
JOptionPane.showMessageDialog(null, "Grabacion realizada con exito");
conecta.resulset= conecta.statement.executeQuery("select * from ciudad order by codigo_producto");
//actualiza_cb_ciudad();
conecta.resulset.first();
//mostrar_datos();
}
catch (SQLException e)
{
JOptionPane.showMessageDialog(null,"Error al intentar grabar el registro "+e);
}
    }//GEN-LAST:event_btn_grabarActionPerformed

    private void btn_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_salirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btn_salirActionPerformed

    private void btn_salirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_salirMouseClicked
        System.exit(0);// TODO add your handling code here:
    }//GEN-LAST:event_btn_salirMouseClicked

    private void btn_borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_borrarActionPerformed
    }
    private static class tf_codigo {

        private static String getText() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        private static void setText(String string) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        private static void setEditable(boolean b) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public tf_codigo() {
        }
    }

    class tf_nombre {

        private static void setText(String string) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        private static void requestFocus() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        private static String getText() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public tf_nombre() {
        }
    }

    class cb_precio {

        private static String getSelectedItem() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        private static void setSelectedItem(String string) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public cb_precio() {
        }
   
    }//GEN-LAST:event_btn_borrarActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }catch (ClassNotFoundException ex) {
            java.default.logging.Logger.getLogger(v_principal.class.getName()).log(java.default.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.default.logging.Logger.getLogger(v_principal.class.getName()).log(java.default.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.default.logging.Logger.getLogger(v_principal.class.getName()).log(java.default.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.default.logging.Logger.getLogger(v_principal.class.getName()).log(java.default.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new v_principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_borrar;
    private javax.swing.JButton btn_grabar;
    private javax.swing.JButton btn_modificar;
    private javax.swing.JButton btn_nuevo;
    private javax.swing.JButton btn_salir;
    private javax.swing.JComboBox cb_precio;
    private javax.swing.JLabel imagen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel label_producto;
    private javax.swing.JPanel panel_ingresar;
    private javax.swing.JPanel panel_opciones;
    private javax.swing.JTextField tf_codigo;
    private javax.swing.JTextField tf_nombre;
    private javax.swing.JLabel title_formulario;
    // End of variables declaration//GEN-END:variables
}